package top.livepan.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.livepan.server.BaseController;
import com.livepan.server.error.BusinessException;
import com.livepan.server.response.CommonReturnType;
import com.livepan.server.service.ResourceService;
import com.livepan.server.service.model.ResourceModel;
import com.livepan.server.service.model.PageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.misc.REException;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/resource")
public class ResourceController extends BaseController {

    public static final String BUSINESS_NAME = "资源";

    private static final Logger LOG = LoggerFactory.getLogger(ResourceController.class);

    @Autowired
    private ResourceService resourceService;

    @ResponseBody
    @RequestMapping("/list")
    public CommonReturnType resource(PageModel<ResourceModel> pageModel) {
        LOG.info("page:" + pageModel.getPage());
        resourceService.list(pageModel);
        return CommonReturnType.create(pageModel);
    }

    @ResponseBody
    @RequestMapping("/allResource")
    public CommonReturnType allResource() {
        return CommonReturnType.create(resourceService.getAllResource());
    }

    @ResponseBody
    @RequestMapping("/saveResourceConfig")
    public CommonReturnType saveResourceConfig(@RequestBody String jsonStr) {
        LOG.info(jsonStr);
        resourceService.saveJsonConfig(jsonStr);
        return CommonReturnType.create(null);
    }


    @ResponseBody
    @RequestMapping("/save")
    public CommonReturnType saveResource(@RequestBody ResourceModel resourceModel) throws BusinessException {
        resourceService.save(resourceModel);
        return CommonReturnType.create(resourceModel);
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public CommonReturnType delete(@PathVariable String id) {
        resourceService.delete(id);
        return CommonReturnType.create(null);
    }


}
